<template>
  <el-card>
    <!-- 面板屑导航 -->
    <Bread
      first="个人中心"
      second="重置密码"
      style="margin-bottom: 20px"
    ></Bread>
    <el-form
      :model="pwdForm"
      :rules="pwdRules"
      ref="pwdRef"
      style="width: 500px"
      label-width="100px"
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input type="password" v-model="pwdForm.old_pwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input type="password" v-model="pwdForm.new_pwd"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input type="password" v-model="pwdForm.re_pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatePwdFn">修改密码</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import { updatedpwd } from '../../api/user'
export default {
  name: '',
  data () {
    const checkNewPwd = (rule, value, next) => {
      if (value === this.pwdForm.old_pwd) {
        return next(new Error('新密码不能和原密码一致'))
      }
      next()
    }
    // 检验两次新密码是否一致
    const checkSamePwd = (rule, value, next) => {
      if (value !== this.pwdForm.new_pwd) {
        return next(new Error('两次输入的密码不一致'))
      }
      next()
    }
    return {
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      pwdRules: {
        old_pwd: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15位的非空字符串',
            trigger: 'blur'
          }
        ],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15位的非空字符串',
            trigger: 'blur'
          },
          { validator: checkNewPwd, trigger: 'blur' }
        ],
        re_pwd: [
          { required: true, message: '请再次输入新密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15位的非空字符串',
            trigger: 'blur'
          },
          {
            validator: checkSamePwd,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {},
  computed: {},
  methods: {
    updatePwdFn () {
      this.$refs.pwdRef.validate(async (valid) => {
        if (valid) {
          const res = await updatedpwd(this.pwdForm)
          console.log(res)
          if (res.data.code !== 0) {
            this.$message({
              type: 'success',
              message: '更新用户密码失败'
            })
            // localStorage.setItem('token', '')
            // this.$router.push('/login')
          } else {
            this.$message({
              type: 'success',
              message: '更新用户密码成功'
            })
            localStorage.setItem('token', '')
            this.$router.push('/login')
          }
        }
      })
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
